package com.cwl.action;


import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;
import com.cwl.domain.User;
import com.cwl.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class UserAction extends ActionSupport implements ModelDriven<User>{
	private User user= new User();
	private UserService userservice;
	private String message;
	public String login(){
		User newuser = new User();
		BeanUtils.copyProperties(this.getModel(), newuser);
		List<User> user1 = userservice.select(newuser);
		System.out.println(user1.size());
		if(user1.size()!=0){
			this.getModel().setId(user1.get(0).getId());
			ServletActionContext.getContext().getSession().put("id", user1.get(0).getId());
			return "ok1";
		}
		return "ok2";
	}
	public String updata(){
		Integer id = (Integer)ServletActionContext.getContext().getSession().get("id");
		User newuser = this.userservice.selectById(id);
		newuser.setScore(new Integer(ServletActionContext.getRequest().getParameter("score")));
		this.userservice.updata(newuser);
		return "updata";
	}
	public String register(){
		return "register";	
	}

	public String registerok(){
		User newuser = new User();
		BeanUtils.copyProperties(this.getModel(), newuser);
		userservice.add(newuser);
		return "ok2";
		
	}

	
	public String getMessage() {
		return message;
	}
	public String checkUsername(){
		User user = this.userservice.selectByName(this.getModel().getName());
		if(user==null){
			System.out.println("1");
			ActionContext.getContext().getValueStack().push("用户名可以使用");
//			this.message="用户名可以使用";
		}else{
			System.out.println("2");
			ActionContext.getContext().getValueStack().push("该用户名已经存在");
//			this.message="该用户名已经存在";
		}
		return SUCCESS;
	}
	
	public User getModel() {
		// TODO Auto-generated method stub
		return user;
	}
	public UserService getUserservice() {
		return userservice;
	}
	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}
}
